package com.cms.recursoshumanos.ui.acceso;

import com.cms.recursoshumanos.component.base.CustomButtonColored;
import com.cms.recursoshumanos.component.base.CustomEditFieldColored;
import com.cms.recursoshumanos.component.base.CustomPasswordField;
import com.cms.recursoshumanos.component.base.TwoColumnField;
import com.cms.recursoshumanos.constant.Constant;
import com.cms.recursoshumanos.container.ForegroundManager;
import com.cms.recursoshumanos.container.TwoColumnFieldManager;
import com.cms.recursoshumanos.engine.ScreenEngine;
import com.cms.recursoshumanos.handler.LoginScreenHandler;
import com.cms.recursoshumanos.popup.ShowDialogAlert;
import com.cms.recursoshumanos.ui.RecursosHumanosScreen;
import com.cms.recursoshumanos.utilities.Tool;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.PasswordEditField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.decor.Border;
import net.rim.device.api.ui.decor.BorderFactory;

/**
 * A class extending the MainScreen class, which provides default standard
 * behavior for BlackBerry GUI applications.
 */
public final class LoginScreen extends RecursosHumanosScreen
{
    /**
     * Creates a new MyScreen object
     */
	private CustomButtonColored boton_login, boton_cancel, boton_recuperar;
	private CustomEditFieldColored edit_username;
	private CustomPasswordField edit_password;
	private HorizontalFieldManager hfm_boton_login;
	
	public static LoginScreen instance = null;
	
	public static LoginScreen getInstance()
	{
		if (instance == null)
		{
			instance = new LoginScreen();
		}
		return instance;
	}
	public LoginScreen() 
	{
		super(NO_VERTICAL_SCROLL | USE_ALL_HEIGHT );
		setComponents();
	}
	
	
	private int validateFields() 
	{
		if(edit_username.getText().trim().equals(""))
		{
			return 1;
		}
		if(edit_password.getText().trim().equals(""))
		{
			return 2;
		}
		return 0;
	}
	public void limpiar_field()
	{
		edit_username.setText("");
		edit_password.setText("");
		invalidate();
	}
	public void setComponents() 
	{
		setTitle("Login");
		
		
		ForegroundManager foreground = new ForegroundManager();
        TwoColumnFieldManager twoColumnManager = new TwoColumnFieldManager( Display.getWidth() / 3, USE_ALL_WIDTH );
        twoColumnManager.setBorder( BorderFactory.createBitmapBorder( new XYEdges( 10, 10, 10, 10 ), Bitmap.getBitmapResource( "border_simple_white.png" ) ) );
        twoColumnManager.setMargin( 10, 10, 10, 10 );
        twoColumnManager.setPadding( 6, 6, 6, 6 );
        
        BitmapField foto_recursos = new BitmapField(
        		Tool.getInstance().sizeImage(Bitmap.getBitmapResource("img/piramid.png"), 
        				Tool.getInstance().getImgWidth(), 
        				Tool.getInstance().getImgHeight()).getBitmap(), FIELD_HCENTER);
        
        twoColumnManager.add(foto_recursos);
        Border editBorder = BorderFactory.createBitmapBorder( new XYEdges( 10, 9, 9, 11 ), Bitmap.getBitmapResource( "border_edit.png" ) );

        Field left;
        Field both;
        
        left = new LabelField( "Usuario", FIELD_LEFT | FIELD_VCENTER );
        
        edit_username = new CustomEditFieldColored( FIELD_VCENTER|EditField.NO_NEWLINE, Constant.COLOR_EDIT_FOCUS);
        edit_username.setBorder( editBorder );
        edit_username.setMargin( 0, 0, 0, 6 );
        
        both = new TwoColumnField( left, edit_username, USE_ALL_WIDTH );
        both.setMargin( 6, 0, 6, 0 );
        twoColumnManager.add( both );
        

        left = new LabelField( "Password", FIELD_LEFT | FIELD_VCENTER );
        
        edit_password = new CustomPasswordField("","",25,FIELD_VCENTER|PasswordEditField.NO_NEWLINE, Constant.COLOR_EDIT_FOCUS);
        edit_password.setBorder( editBorder );
        edit_password.setMargin( 0, 0, 0, 6 );
        
        both = new TwoColumnField( left, edit_password, USE_ALL_WIDTH );
        both.setMargin( 6, 0, 6, 0 );
        twoColumnManager.add( both );
           
        Field separator = new SeparatorField();
        separator.setMargin( 6, 0, 6, 0 );
        twoColumnManager.add( separator );
        
        hfm_boton_login = new HorizontalFieldManager(Field.FIELD_HCENTER); 
        
        boton_login = new CustomButtonColored("Login",ButtonField.CONSUME_CLICK, Constant.COLOR_BUTTON_FOCUS); 
        boton_cancel = new CustomButtonColored("Resetear",ButtonField.CONSUME_CLICK, Constant.COLOR_BUTTON_FOCUS); 
        boton_recuperar = new CustomButtonColored("Problemas Para Ingresar",ButtonField.CONSUME_CLICK | USE_ALL_WIDTH, Constant.COLOR_BUTTON_FOCUS); 
        
        boton_login.setChangeListener(new FieldChangeListener() 
        {
	   		public void fieldChanged(Field field, int context) 
	       	{ 
	   			
	   			ShowDialogAlert alert;
	   			if(isConected()) 
	       		{
	   	    		switch(validateFields()) 
	   	    		{
	   		    		case 0:
	   		    			
   		    					String strUsername = edit_username.getText().toString().trim();
	   		    		 		String strPassword = edit_password.getText().toString().trim();

	   		    		 		LoginScreenHandler handler = new LoginScreenHandler();
	   		    		 		handler.LoginSistema(strUsername, strPassword);

	   		    			break;
	   		    		case 1:
	   		    			alert = new ShowDialogAlert("Usuario Requerido");
	   		    			alert.run();
	   		    			edit_username.setFocus();
	   		    			break;
	   		    		case 2:
	   		    			alert = new ShowDialogAlert("Password Requerido");
	   		    			alert.run();
	   		    			edit_password.setFocus();
	   		    			break;
	   		    		default:
	   		    			alert = new ShowDialogAlert("Default case");
	   		    			alert.run();
	   		    			break;
	   		    		}
	   	    		
	       		} 
	       		else 
	       		{
	       			alert = new ShowDialogAlert("No Hay Coneccion");
	       			alert.run();
	       			onClose();
	       		}
	       	} 
		}); 
        boton_cancel.setChangeListener(new FieldChangeListener() 
        {
				public void fieldChanged(Field field, int context)
		    	{ 
		    		edit_username.setText("");
		    		edit_password.setText("");
		    		edit_username.setFocus();
		    		invalidate();
		   		} 
		}); 
        boton_recuperar.setChangeListener(new FieldChangeListener() 
        {
				public void fieldChanged(Field field, int context)
		    	{ 
		    		ScreenEngine.getInstance().goPantallaMainRecuperacion();
		   		} 
		}); 
        
        hfm_boton_login.add(boton_login); 
        hfm_boton_login.add(boton_cancel);  
        
        twoColumnManager.add( hfm_boton_login );
        twoColumnManager.add( boton_recuperar );
        
        foreground.add( twoColumnManager );
        add( foreground );
        
        
			
	}
	
	
}
